﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helpers
{
    /// <summary>
    /// String扩展
    /// </summary>
    public static class StringEx
    {
        
        /// <summary>
        /// 获取str中index位置的char
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static char GetChar(this String str, int index)
        {
            if (!String.IsNullOrEmpty(str))
            {
                return str.ToCharArray()[index];
            }
            return char.MinValue;
        }
        /// <summary>
        /// 获取 str中多处位置的char,并组合成string
        /// </summary>
        /// <param name="str"></param>
        /// <param name="indices"></param>
        /// <returns></returns>
        public static string GetChars(this String str, params int[] indices)
        {
            if (!String.IsNullOrEmpty(str))
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in indices)
                {
                    sb.Append(str.GetChar(item));
                }
                return sb.ToString();
            }
            return null;
        }
        public static char GetLastChar(this String str)
        {
            if(! String.IsNullOrEmpty(str))
                return str.GetChar(str.Length-1);
            return char.MinValue;
        }
        public static char GetFirstChar(this String str)
        {
            return str.GetChar(0);
        }
    }
}
